#include <WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#define OLED_PW 25
#define IRQ2 32
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <RX8010.h>
#include "Wire.h"
#include "tinySHT2x.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128    // OLED display width, in pixels
#define SCREEN_HEIGHT 32    // OLED display height, in pixels
#define OLED_RESET -1       // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
unsigned long lastTime;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char msg[50];
tinySHT2x sht;
WiFiUDP ntpUDP;
// NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 8 * 3600, 60000);
NTPClient timeClient(ntpUDP, "ntp.aliyun.com", 8 * 3600, 60000);
bool intStatus;
dateTime m;
// void IRAM_ATTR interruptIrq2(void);
dateTime timeSync()
{
    dateTime dt;
    timeClient.update();
    dt.year = timeClient.getYear() - 2000;
    dt.month = timeClient.getMonth();
    dt.date = timeClient.getDate();
    dt.day = timeClient.getDay();
    dt.hour = timeClient.getHours();
    dt.minute = timeClient.getMinutes();
    dt.second = timeClient.getSeconds();
    return dt;
}

// OLED控制程序，0为关闭，1为开启
void OledCtrl(bool a)
{
    if (a)
    {
        digitalWrite(OLED_PW, 0);
    }
    else
    {
        digitalWrite(OLED_PW, 1);
    }
}
void IRAM_ATTR interruptIrq2a()
{
    intStatus = 1;
    //    Serial.println(2);
    //       Serial.printf("%02d-%02d-%02d %02d:%02d:%02d %.1fC %.1f%%\n", m.year, m.month, m.date, m.hour, m.minute, m.second, sht.getTemperature(), sht.getHumidity());
}
void setup()
{
    Serial.begin(115200);
    Wire.begin();
    pinMode(OLED_PW, OUTPUT);
    pinMode(IRQ2, INPUT);
    OledCtrl(1);
    if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
    {
        Serial.println(F("SSD1306 allocation failed"));
        OledCtrl(0);
    }
    display.clearDisplay();

    display.setTextSize(1);              // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(0, 0);             // Start at top-left corner
    display.println("Hello :)");
    display.print("Connect WIFI ");
    display.display();
    WiFiManager wifiManager;
    wifiManager.setTimeout(180);
    if (!wifiManager.autoConnect("AutoConnectAP"))
    {
        Serial.println("failed to connect and hit timeout");
        display.println("failed!");
        display.display();
        delay(3000);
    }
    else
    {
        Serial.println("connected...yeey :)");
        display.println("OK");
        display.display();
        delay(500);
    }
    timeClient.begin();
    dateTime dt;
    if (timeClient.update())
    {
        Serial.print("Time sync OK  ");
        display.println("Time sync OK!");
        display.display();
        dt = timeSync();
        Serial.printf("%02d-%02d-%02d %02d:%02d:%02d\n", dt.year, dt.month, dt.date, dt.hour, dt.minute, dt.second);
        //    resetRX8010();
        setTime(dt);
    }
    else
    {
        Serial.println("Time sync ERROR");
        display.println("Time sync ERROR!");
        display.display();
    }
    enableFOUT(1, 0);
    sht.begin();
    delay(500);
    attachInterrupt(IRQ2, interruptIrq2a, RISING); // 当电平发生变化时，触发中断
    Serial.println(1);
    intStatus = 0;
}

void loop()
{

    if (intStatus == 1)
    {
        m = getDateTime();

        //
        //    Serial.printf("%02d-%02d-%02d %02d:%02d:%02d %.1fC %.1f%%\n", m.year, m.month, m.date, m.hour, m.minute, m.second, sht.getTemperature(), sht.getHumidity());
        sprintf(msg, "20%02d-%02d-%02d %02d:%02d:%02d\n%.2fC %.2f%%", m.year, m.month, m.date, m.hour, m.minute, m.second, sht.getTemperature(), sht.getHumidity());
        display.clearDisplay();
        display.setCursor(0, 0); // Start at top-left corner
        display.println(msg);
        display.display();
        Serial.println(msg);
        intStatus = 0;
    }
    //  delay(500);
    //   rtc_wdt_feed();
    //   Serial.println("a");
}
